package net.asfun.jvalog.control;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.entity.Paper;
import net.asfun.jvalog.service.PageService;
import net.asfun.jvalog.util.RequestUtil;
import net.asfun.jvalog.util.ResponseUtil;
import net.asfun.jvalog.vo.Pager;
import net.asfun.jvalog.mvc.RequestMapping;

public class PageController {

	
	@RequestMapping("/page")
	public String index(HttpServletRequest req, Map<String, Object> model) {
		int page = RequestUtil.getIntParam(req, "page");
		Pager pager = new Pager(page);
		model.put("pages", PageService.listAll(pager));
		model.put("pager", pager);
		return "pages.html";
	}
	
	@RequestMapping("/page/create")
	public String create(HttpServletRequest req, Map<String, Object> model) {
		return "page.html";
	}
	
	@RequestMapping("/page/add")
	public String add(HttpServletRequest req, Map<String, Object> model) {
		String title = RequestUtil.getRequiredParam(req, "title");
		String slogan =  RequestUtil.getStringParam(req, "slogan", title);
		String article = RequestUtil.getRequiredParam(req, "article");
		String excerpt = RequestUtil.getStringParam(req, "excerpt");
		String publish = RequestUtil.getStringParam(req, "publish");
		
		Paper paper = new Paper();
		paper.setType(Paper.PAGE_TYPE);
		paper.setContent(article);
		paper.setSlogan(slogan);
		paper.setTitle(title);
		paper.setExcerpt(excerpt);
		paper.setDate(new Date());
		if ( publish.length() > 0 ) {
			paper.setPublished(true);
		}
		PageService.addPage(paper);
		
		return ResponseUtil.feedback("page.add", req, model);
	}
	
	@RequestMapping("/page/delete")
	public String delete(HttpServletRequest req, Map<String, Object> model) {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for(int i=0; i<ids.length; i++) {
			PageService.delete(ids[i]);
		}
		return ResponseUtil.feedback("page.delete", req, model);
	}
	
	@RequestMapping("/page/update")
	public String update(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		String title = RequestUtil.getRequiredParam(req, "title");
		String slogan =  RequestUtil.getStringParam(req, "slogan", title);
		String article = RequestUtil.getStringParam(req, "article");
		String excerpt = RequestUtil.getStringParam(req, "excerpt");
		String publish = RequestUtil.getStringParam(req, "publish");
		
		Paper paper = new Paper();
		paper.setId(id);
		paper.setTitle(title);
		paper.setSlogan(slogan);
		if ( article.length() > 0 ) {
			paper.setContent(article);
		}
		paper.setExcerpt(excerpt);
		if ( publish.length() > 0 ) {
			paper.setPublished(true);
		} else {
			paper.setPublished(false);
		}
		PageService.update(paper);
		
		return ResponseUtil.feedback("page.update", req, model);
	}
	
	@RequestMapping("/page/edit")
	public String edit(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		model.put("page", PageService.getById(id));
		return "page.html";
	}
	
}
